package com.nopay.cdxphone.dialog;

import android.content.Context;
import android.graphics.Point;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.ListView;

import com.zyyoona7.popup.BasePopup;
import com.zyyoona7.popup.XGravity;

import java.util.List;

import com.nopay.cdxphone.R;
import com.nopay.cdxphone.bean.AreaBean;
import com.nopay.cdxphone.bean.response.AreaInfoResponse;
import com.nopay.cdxphone.ui.adapter.AreaAdapter;

/**
 * author：jun
 * date：2021/7/16
 */
public class AreaPopup extends BasePopup<AreaPopup> {

    private final int mScreenWidth;
    private AreaAdapter adapter;

    public AreaPopup(Context context) {
        super();
        setContext(context);
        WindowManager windowManager = (WindowManager) (context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE));
        Display display = windowManager.getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);
        mScreenWidth = point.x;
        adapter = new AreaAdapter(context);
        adapter.setOnProvinceClickListener((v, position) -> {
            boolean isSelected = adapter.getItem(position).isSelected();
            if(isSelected) {
                for (int i = 0; i < adapter.getItem(position).getCityList().size(); i++) {
                    adapter.getItem(position).getCityList().get(i).setSelected(false);
                }
            }
            adapter.getItem(position).setSelected(!isSelected);
            adapter.notifyDataSetChanged();
        });
    }

    @Override
    protected void initAttributes() {
        int width = mScreenWidth * 2 / 3;
        setContentView(R.layout.popup_area, width, ViewGroup.LayoutParams.MATCH_PARENT);
        setFocusAndOutsideEnable(true)
                .setBackgroundDimEnable(true)
                .setAnimationStyle(R.style.AreaPopupAnim)
                .setXGravity(XGravity.ALIGN_RIGHT);
    }

    @Override
    protected void initViews(View view, AreaPopup popup) {
        findViewById(R.id.tv_sure).setOnClickListener(v -> {
            dismiss();
            CheckBox cb_unlimited = findViewById(R.id.cb_unlimited);
            boolean isAll = cb_unlimited.isChecked();
            if(!isAll) {
                if(onAreaConfirmListener != null) onAreaConfirmListener.onAreaConfirm(adapter.getSelectedArea());
                return;
            }

            if(onAreaConfirmListener != null) onAreaConfirmListener.onAreaConfirm(null);
        });

        ListView lv_province = findViewById(R.id.lv_province);
        lv_province.setAdapter(adapter);
    }

    public AreaPopup setData(List<AreaBean> beans) {
        adapter.addList(beans);
        return this;
    }

    private OnAreaConfirmListener onAreaConfirmListener;

    public AreaPopup setOnAreaConfirmListener(OnAreaConfirmListener onAreaConfirmListener) {
        this.onAreaConfirmListener = onAreaConfirmListener;
        return this;
    }

    public interface OnAreaConfirmListener {

        void onAreaConfirm(List<AreaInfoResponse.Data> datas);
    }
}
